package com.example.zhihuhelpapp.utils;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;

import androidx.annotation.StringRes;

public class UIUtils {
    public static void showToast(String message){
        Toast.makeText(CoreLib.getContext(),message,Toast.LENGTH_SHORT).show();
    }
    public static void showToast(@StringRes int message){
        Toast.makeText(CoreLib.getContext(),message,Toast.LENGTH_SHORT).show();
    }

    /**
     * 实现粘贴功能
     */
    public static String getPastContent(Context context) {
        ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (manager != null) {
            ClipData data = manager.getPrimaryClip();
            if (data != null) {
                ClipData.Item item = data.getItemAt(0);
                return item.getText().toString();
            }
        }
        return "";
    }

    /**
     * 实现文本复制功能
     */
    public static void copy(Context context, String content) {
        // 得到剪贴板管理器
        ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (manager != null) {
            ClipData data = ClipData.newPlainText(ClipDescription.MIMETYPE_TEXT_PLAIN, content);
            manager.setPrimaryClip(data);
        }
    }
}
